home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BLWCB15A.ZIP / BFISH4.PAS next >
Pascal/Delphi Source File  |  1996-08-27  |  35KB  |  596 lines

  1. unit Bfish4;
  2.  
  3. (* IF you don't have an x86 processor THEN comment the following directive *)
  4. (* {$DEFINE Use_Assembler} *)
  5.  
  6.  (* ---------------------------------------------------------------------- *)
  7.  (*                                                                        *)
  8.  (*  The original source code in C as published in Dr. Dobb's Journal  of  *)
  9.  (*  April  1994  used  one  variable containing the  P-Array and another  *)
  10.  (*  variable containing the four S-boxes.                                 *)
  11.  (*                                                                        *)
  12.  (*  In this implementation only the datatypes of these two are  defined.  *)
  13.  (*  This  enables the user to initialize multiple keys concurrently, but  *)
  14.  (*  requires the calling routines to have  the  used  variables  defined  *)
  15.  (*  themselves.                                                           *)
  16.  (*                                                                        *)
  17.  (*  The  author of the Blowfish algorithm can be reached via internet as  *)
  18.  (*  schneier@chinet.com                                                   *)
  19.  (*                                                                        *)
  20.  (*  This units author can be reached via the following E-Mail addresses:  *)
  21.  (*                                                                        *)
  22.  (*      Via InterNet    : dutra.lacerda@mail.telepac.pt                   *)
  23.  (*      Via FidoNet     : Dutra Lacerda @2:362/20                         *)
  24.  (*      Via Compuserve  : 100342,2501                                     *)
  25.  (*                                                                        *)
  26.  (* ---------------------------------------------------------------------- *)
  27.  
  28. interface
  29.  
  30. const
  31.   bf_N  = 16;          {  16 is the standart value. MAXIMUM is 111 !!!  }
  32.   bf_MAX= 111;         { Maximum number of rounds... P-Boxes=111+2=113  }
  33.   bf_IV = $AAAAAAAA;   { In the original algorithm this vector was zero }
  34.  
  35. const
  36.   PBoxSizeL  = bf_N+2;
  37.   SBoxSizeL  = 4*256;
  38.   LIntSizeB  = 4;
  39.   PBoxSizeB  = LIntSizeB * PBoxSizeL;
  40.   SBoxSizeB  = LIntSizeB * SBoxSizeL;
  41.   HighestNumber = 294967296;
  42.  
  43.  
  44. type
  45.   PArray = array[0..(bf_N+2)-1] of LongInt;
  46.   SBox = array[0..3, 0..255] of LongInt;
  47.   LArray    = array[0..3] of Byte;
  48.   PLArray   = ^LArray;
  49.   TSeedBuffer = Record
  50.                   Case boolean of
  51.                   True :
  52.                     ( byte : Array[0..5003] of byte);
  53.                   False:
  54.                     ( long : Array[0..1250] of LongInt);
  55.                 end;
  56.  
  57.  
  58.  
  59. procedure InitBlowFish(Key : String; var P : PArray; var S : SBox);
  60.  
  61.   { Initialization of a key is needed before performing any encryption }
  62.   { or decryption of data.                                             }
  63.  
  64.  
  65. function F( Input :LongInt; S :SBox):LongInt;
  66. procedure BlowEncrypt(var Xl, Xr : LongInt; P : PArray; S : SBox);
  67. procedure BlowDecrypt(var Xl, Xr : LongInt; P : PArray; S : SBox);
  68.  
  69.   { These two should speak for themselves, Xl and Xr are the  first  and }
  70.   { the  second  32 bits of data you want to encrypt or decrypt, P and S }
  71.   { are the P-array and the S-boxes which you currently are using.       }
  72.   {                                                                      }
  73.   { Note that the data is not  copied  or  stored  before  the  Blowfish }
  74.   { algorithm  is  applied  to it and that you have to perform a loop in }
  75.   { order to encrypt or decrypt more data than 8 bytes.                  }
  76.  
  77.  
  78. implementation
  79.  
  80. const
  81.   RandomSeed : TSeedBuffer = ( byte : (
  82.   36,  63, 106, 136, 133, 163,   8, 211,  19,  25, 138,  46,   3, 112, 115,  68,
  83.  164,   9,  56,  34,  41, 159,  49, 208,   8,  46, 250, 152, 236,  78, 108, 137,
  84.   69,  40,  33, 230,  56, 208,  19, 119, 190,  84, 102, 207,  52, 233,  12, 108,
  85.  192, 172,  41, 183, 201, 124,  80, 221,  63, 132, 213, 181, 181,  71,   9,  23,
  86.  146,  22, 213, 217, 137, 121, 251,  27, 209,  49,  11, 166, 152, 223, 181, 172,
  87.   47, 253, 114, 219, 208,  26, 223, 183, 184, 225, 175, 237, 106,  38, 126, 150,
  88.  186, 124, 144,  69, 241,  44, 127, 153,  36, 161, 153,  71, 179, 145, 108, 247,
  89.    8,  1,  242, 226, 133, 142, 252,  22, 99,  105,  32, 216, 113,  87,  78, 105,
  90.  164,  88, 254, 163, 244, 147,  61, 126,  13, 149, 116, 143, 114, 142, 182,  88,
  91.  113, 139, 205,  88, 130,  21,  74, 238, 123,  84, 164,  29, 194,  90,  89, 181,
  92.  156,  48, 213,  57,  42, 242,  96,  19, 197, 209, 176,  35,  40,  96, 133, 240,
  93.  202,  65, 121,  24, 184, 219,  56, 239, 142, 121, 220, 176,  96,  58,  24,  14,
  94.  108, 158,  14, 139, 176,  30, 138,  62, 215,  21, 119, 193, 189,  49,  75,  39,
  95.  120, 175,  47, 218,  85,  96,  92,  96, 230,  85,  37, 243, 170,  85, 171, 148,
  96.   87,  72, 152,  98,  99, 232,  20,  64,  85, 202,  57, 106,  42, 171,  16, 182,
  97.  180, 204,  92,  52,  17,  65, 232, 206, 161,  84, 134, 175, 124, 114, 233, 147,
  98.  179, 238,  20,  17,  99, 111, 188,  42,  43, 169, 197,  93, 116,  24,  49, 246,
  99.  206,  92,  62,  22, 155, 135, 147,  30, 175, 214, 186,  51, 108,  36, 207,  92,
  100.  122,  50,  83, 129,  40, 149, 134, 119,  59, 143,  72, 152, 107,  75, 185, 175,
  101.  196, 191, 232,  27, 102,  40,  33, 147,  97, 216,   9, 204, 251,  33, 169, 145,
  102.   72, 124, 172,  96,  93, 236, 128,  50, 239, 132,  93,  93, 233, 133, 117, 177,
  103.  220,  38,  35,   2, 235, 101,  27, 136,  35, 137,  62, 129, 211, 150, 172, 197,
  104.   15, 109, 111, 243, 131, 244,  66,  57,  46,  11,  68, 130, 164, 132,  32,   4,
  105.  105, 200, 240,  74, 158,  31, 155,  94,  33, 198, 104,  66, 246, 233, 108, 154,
  106.  103,  12, 156,  97, 171, 211, 136, 240, 106,  81, 160, 210, 216,  84,  47, 104,
  107.  150,  15, 167,  40, 171,  81,  51, 163, 110, 239,  11, 108,  19, 122,  59, 228,
  108.  186,  59, 240,  80, 126, 251,  42, 152, 161, 241, 101,  29,  57, 175,   1, 118,
  109.  102, 202,  89,  62, 130,  67,  14, 136, 140, 238, 134,  25,  69, 111, 159, 180,
  110.  125, 132, 165, 195,  59, 139,  94, 190, 224, 111, 117, 216, 133, 193,  32, 115,
  111.   64,  26,  68, 159,  86, 193, 106, 166,  78, 211, 170,  98,  54,  63, 119,   6,
  112.   27, 254, 223, 114,  66, 155,   2,  61,  55, 208, 215,  36, 208,  10,  18,  72,
  113.  219,  15, 234, 211,  73, 241, 192, 155,   7,  83, 114, 201, 128, 153,  27, 123,
  114.   37, 212, 121, 216, 246, 232, 222, 247, 227, 254,  80,  26, 182, 121,  76,  59,
  115.  151, 108, 224, 189,   4, 192,   6, 186, 193, 169,  79, 182,  64, 159,  96, 196,
  116.   94,  92, 158, 194,  25, 106,  36,  99, 104, 251, 111, 175,  62, 108,  83, 181,
  117.   19,  57, 178, 235,  59,  82, 236, 111, 109, 252,  81,  31, 155,  48, 149,  44,
  118.  204, 129,  69,  68, 175,  94, 189,   9, 190, 227, 208,   4, 222,  51,  74, 253,
  119.  102,  15,  40,   7,  25,  46,  75, 179, 192, 203, 168,  87,  69, 200, 116,  15,
  120.  210,  11,  95,  57, 185, 211, 251, 219,  85, 121, 192, 189,  26,  96,  50,  10,
  121.  214, 161,   0, 198,  64,  44, 114, 121, 103, 159,  37, 254, 251,  31, 163, 204,
  122.  142, 165, 233, 248, 219,  50,  34, 248,  60, 117,  22, 223, 253,  97, 107,  21,
  123.   47,  80,  30, 200, 173,   5,  82, 171,  50,  61, 181, 250, 253,  35, 135,  96,
  124.   83,  49, 123,  72,  62,   0, 223, 130, 158,  92,  87, 187, 202, 111, 140, 160,
  125.   26, 135,  86,  46, 223,  23, 105, 219, 213,  66, 168, 246,  40, 126, 255, 195,
  126.  172, 103,  50, 198, 140,  79,  85, 115, 105,  91,  39, 176, 187, 202,  88, 200,
  127.  225, 255, 163,  93, 184, 240,  17, 160,  16, 250,  61, 152, 253,  33, 131, 184,
  128.   74, 252, 181, 108,  45, 209, 211,  91, 154,  83, 228, 121, 182, 248,  69, 101,
  129.  210, 142,  73, 188,  75, 251, 151, 144, 225, 221, 242, 218, 164, 203, 126,  51,
  130.   98, 251,  19,  65, 206, 228, 198, 232, 239,  32, 202, 218,  54, 119,  76,   1,
  131.  208, 126, 158, 254,  43, 241,  31, 180, 149, 219, 218,  77, 174, 144, 145, 152,
  132.  234, 173, 142, 113, 107, 147, 213, 160, 208, 142, 209, 208, 175, 199,  37, 224,
  133.  142,  60,  91,  47, 142, 117, 148, 183, 143, 246, 226, 251, 242,  18,  43, 100,
  134.  136, 136, 184,  18, 144,  13, 240,  28,  79, 173,  94, 160, 104, 143, 195,  28,
  135.  209, 207, 241, 145, 179, 168, 193, 173,  47,  47,  34,  24, 190,  14,  23, 119,
  136.  234, 117,  45, 254, 139,   2,  31, 161, 229, 160, 204 , 15, 181, 111, 116, 232,
  137.   24, 172, 243, 214, 206, 137, 226, 153, 180, 168,  79, 224, 253,  19, 224, 183,
  138.  124, 196,  59, 129, 210, 173, 168, 217,  22,  95, 162, 102, 128, 149, 119,   5,
  139.  147, 204, 115,  20,  33,  26,  20, 119, 230, 173,  32, 101, 119, 181, 250, 134,
  140.  199,  84,  66, 245, 251, 157,  53, 207, 235, 205, 175,  12, 123,  62, 137, 160,
  141.  214,  65,  27, 211, 174,  30, 126,  73,   0,  37,  14,  45,  32, 113, 179,  94,
  142.   34, 104,   0, 187,  87, 184, 224, 175,  36, 100,  54, 155, 240,   9, 185,  30,
  143.   85,  99, 145,  29,  89, 223, 166, 170, 120, 193,  67, 137, 217,  90,  83, 127,
  144.   32, 125,  91, 162,   2, 229, 185, 197, 131,  38,   3, 118,  98, 149, 207, 169,
  145.   17, 200,  25, 104,  78, 115,  74,  65, 179,  71,  45, 202, 123,  20, 169,  74,
  146.   27,  81,   0,  82, 154,  83,  41,  21, 214,  15,  87,  63, 188, 155, 198, 228,
  147.   43,  96, 164, 118, 129, 230, 116,   0,   8, 186, 111, 181,  87,  27, 233,  31,
  148.  242, 150, 236, 107,  42,  13, 217,  21, 182,  99, 101,  33, 231, 185, 249, 182,
  149.  255,  52,   5,  46, 197, 133,  86, 100,  83, 176,  45,  93, 169, 159, 143, 161,
  150.    8, 186,  71, 153, 110, 133,   7, 106,  75, 122, 112, 233, 181, 179,  41,  68,
  151.  219, 117,   9,  46, 196,  25,  38,  35, 173, 110, 166, 176,  73, 167, 223, 125,
  152.  156, 238,  96, 184, 143, 237, 178, 102, 236, 170, 140, 113, 105, 154,  23, 255,
  153.   86, 100,  82, 108, 194, 177, 158, 225,  25,  54,   2, 165, 117,   9,  76,  41,
  154.  160,  89,  19,  64, 228,  24,  58,  62,  63,  84, 152, 154,  91,  66, 157, 101,
  155.  107, 143, 228, 214, 153, 247,  63, 214, 161, 210, 156,   7, 239, 232,  48, 245,
  156.   77,  45,  56, 230, 240,  37,  93, 193,  76, 221,  32, 134, 132, 112, 235,  38,
  157.   99, 130, 233, 198,   2,  30, 204,  94,   9, 104, 107,  63,  62, 186, 239, 201,
  158.   60, 151,  24,  20, 107, 106, 112, 161, 104, 127,  53, 132,  82, 160, 226, 134,
  159.  183, 156,  83,   5, 170,  80,   7,  55,  62,   7, 132,  28, 127, 222, 174,  92,
  160.  142, 125,  68, 236,  87,  22, 242, 184, 176,  58, 218,  55, 240,  80,  12,  13,
  161.  240,  28,  31,   4,   2,   0, 179, 255, 174,  12, 245,  26,  60, 181, 116, 178,
  162.   37, 131, 122,  88, 220,   9,  33, 189, 209, 145,  19, 249, 124, 169,  47, 246,
  163.  148,  50,  71, 115,  34, 245,  71,   1,  58, 229, 229, 129,  55, 194, 218, 220,
  164.  200, 181, 118,  52, 154, 243, 221, 167, 169,  68,  97,  70,  15, 208,   3,  14,
  165.  236, 200, 199,  62, 164, 117,  30,  65, 226,  56, 205, 153,  59, 234,  14,  47,
  166.   50, 128, 187, 161,  24,  62, 179,  49,  78,  84, 139,  56,  79, 109, 185,   8,
  167.  111,  66,  13,   3, 246,  10,   4, 191,  44, 184,  18, 144,  36, 151, 124, 121,
  168.   86, 121, 176, 114, 188, 175, 137, 175, 222, 154, 119,  31, 217, 147,   8,  16,
  169.  179, 139, 174,  18, 220, 207,  63,  46,  85,  18, 114,  31,  46, 107, 113,  36,
  170.   80,  26, 221, 230, 159, 132, 205, 135, 122,  88,  71,  24, 116,   8, 218,  23,
  171.  188, 159, 154, 188, 233,  75, 125, 140, 236, 122, 236,  58, 219, 133,  29, 250,
  172.   99,   9,  67, 102, 196, 100, 195, 210, 239,  28,  24,  71,  50,  21, 217,   8,
  173.  221,  67,  59,  55,  36, 194, 186,  22,  18, 161,  77,  67,  42, 101, 196,  81,
  174.   80, 148,   0,   2,  19,  58, 228, 221, 113, 223, 248, 158,  16,  49,  78,  85,
  175.  129, 172, 119, 214,  95,  17,  25, 155,   4,  53,  86, 241, 215, 163, 199, 107,
  176.   60,  17,  24,  59,  89,  36, 165,   9, 242, 143, 230, 237, 151, 241, 251, 250,
  177.  158, 186, 191,  44,  30,  21,  60, 110, 134, 227,  69, 112, 234, 233, 111, 177,
  178.  134,  14,  94,  10,  90,  62,  42, 179, 119,  31, 231,  28,  78,  61,   6, 250,
  179.   41, 101, 220, 185, 153, 231,  29,  15, 128,  62, 137, 214,  82, 102, 200,  37,
  180.   46,  76, 201, 120, 156,  16, 179, 106, 198,  21,  14, 186, 148, 226, 234, 120,
  181.  165, 252,  60,  83,  30,  10,  45, 244, 242, 247,  78, 167,  54,  29,  43,  61,
  182.   25,  57,  38,  15,  25, 194, 121,  96,  82,  35, 167,   8, 247,  19,  18, 182,
  183.  235, 173, 254, 110, 234, 195,  31, 102, 227, 188,  69, 149, 166, 123, 200, 131,
  184.  177, 127,  55, 209,   1, 140, 255,  40, 195,  50, 221, 239, 190, 108,  90, 165,
  185.  101,  88,  33, 133, 104, 171, 152,   2, 238, 206, 165,  15, 219,  47, 149,  59,
  186.   42, 239, 125, 173,  91, 110,  47, 132,  21,  33, 182,  40,  41,   7,  97, 112,
  187.  236, 221,  71, 117,  97, 159,  21,  16,  19, 204, 168,  48, 235,  97, 189, 150,
  188.    3,  52, 254,  30, 170,   3,  99, 207, 181, 115,  92, 144,  76, 112, 162,  57,
  189.  213, 158, 158,  11, 203, 170, 222,  20, 238, 204, 134, 188,  96,  98,  44, 167,
  190.  156, 171,  92, 171, 178, 243, 132, 110, 100, 139,  30, 175,  25, 189, 240, 202,
  191.  160,  35, 105, 185, 101,  90, 187,  80,  64, 104,  90,  50,  60,  42, 180, 179,
  192.   49, 158, 233, 213, 192,  33, 184, 247, 155,  84,  11,  25, 135,  95, 160, 153,
  193.  149, 247, 153, 126,  98,  61, 125, 168, 248,  55, 136, 154, 151, 227,  45, 119,
  194.   17, 237, 147,  95,  22, 104,  18, 129,  14,  53, 136,  41, 199, 230,  31, 214,
  195.  150, 222, 223, 161, 120,  88, 186, 153,  87, 245, 132, 165,  27,  34, 114,  99,
  196.  155, 131, 195, 255,  26, 194,  70, 150, 205, 179,  10, 235,  83,  46,  48,  84,
  197.  143, 217,  72, 228, 109, 188,  49,  40,  88, 235, 242, 239,  52, 198, 255, 234,
  198.  254,  40, 237,  97, 238, 124,  60, 115,  93,  74,  20, 217, 232, 100, 183, 227,
  199.   66,  16,  93,  20,  32,  62,  19, 224,  69, 238, 226, 182, 163, 170, 171, 234,
  200.  219, 108,  79,  21, 250, 203,  79, 208, 199,  66, 244,  66, 239, 106, 187, 181,
  201.  101,  79,  59,  29,  65, 205,  33,   5, 216,  30, 121, 158, 134, 133,  77, 199,
  202.  228,  75,  71, 106,  61, 129,  98,  80, 207,  98, 161, 242,  91, 141,  38,  70,
  203.  252, 136, 131, 160, 193, 199, 182, 163, 127,  21,  36, 195, 105, 203, 116, 146,
  204.   71, 132, 138,  11,  86, 146, 178, 133,   9,  91, 191,   0, 173,  25,  72, 157,
  205.   20,  98, 177, 116,  35, 130,  14,   0,  88,  66, 141,  42,  12,  85, 245, 234,
  206.   29, 173, 244,  62,  35,  63, 112,  97,  51, 114, 240, 146, 141, 147, 126,  65,
  207.  214,  95, 236, 241, 108,  34,  59, 219, 124, 222,  55,  89, 203, 238, 116,  96,
  208.   64, 133, 242, 167, 206, 119,  50, 110, 166,   7, 128, 132,  25, 248,  80, 158,
  209.  232, 239, 216,  85,  97, 217, 151,  53, 169, 105, 167, 170, 197,  12,   6, 194,
  210.   90,   4, 171, 252, 128,  11, 202, 220, 158,  68, 122,  46, 195,  69,  52, 132,
  211.  253, 213, 103,   5,  14,  30, 158, 201, 219, 115, 219, 211,  16,  85, 136, 205,
  212.  103,  95, 218, 121, 227, 103,  67,  64, 197, 196,  52, 101, 113,  62,  56, 216,
  213.   61,  40, 248, 158, 241, 109, 255,  32,  21,  62,  33, 231, 143, 176,  61,  74,
  214.  230, 227, 159,  43, 219, 131, 173, 247, 233,  61,  90, 104, 148, 129,  64, 247,
  215.  246,  76,  38,  28, 148, 105,  41,  52,  65,  21,  32, 247, 118,   2, 212, 247,
  216.  188, 244, 107,  46, 212, 162,   0, 104, 212,   8,  36, 113,  51,  32, 244, 106,
  217.   67, 183, 212, 183,  80,   0,  97, 175,  30,  57, 246,  46, 151,  36,  69,  70,
  218.   20,  33,  79, 116, 191, 139, 136,  64,  77, 149, 252,  29, 150, 181, 145, 175,
  219.  112, 244, 221, 211, 102, 160,  47,  69, 191, 188,   9, 236,   3, 189, 151, 133,
  220.  127, 172, 109, 208,  49, 203, 133,   4, 150, 235,  39, 179,  85, 253,  57,  65,
  221.  218,  37,  71, 230, 171, 202,  10, 154,  40,  80, 120,  37,  83,   4,  41, 244,
  222.   10,  44, 134, 218, 233, 182, 109, 251, 104, 220,  20,  98, 215,  72, 105,   0,
  223.  104,  14, 192, 164,  39, 161, 141, 238,  79,  63, 254, 162, 232, 135, 173, 140,
  224.  181, 140, 224,   6, 122, 244, 214, 182, 170, 206,  30, 124, 211,  55,  95, 236,
  225.  206, 120, 163, 153,  64, 107,  42,  66,  32, 254, 158,  53, 217, 243, 133, 185,
  226.  238,  57, 215, 171,  59,  18,  78, 139,  29, 201, 250, 247,  75, 109,  24,  86,
  227.   38, 163, 102,  49, 234, 227, 151, 178,  58, 110, 250, 116, 221,  91,  67,  50,
  228.  104 , 65, 231, 247, 202, 120,  32, 251, 251,  10, 245,  78, 216, 254, 179, 151,
  229.   69,  64,  86, 172, 186,  72, 149,  39,  85,  83,  58,  58,  32, 131, 141, 135,
  230.  254, 107, 169, 183, 208, 150, 149,  75,  85, 168, 103, 188, 161,  21, 154,  88,
  231.  204, 169,  41,  99, 153, 225, 219,  51, 166,  42,  74,  86,  63,  49,  37, 249,
  232.   94, 244, 126,  28, 144,  41,  49, 124, 253, 248, 232,   2,   4,  39,  47, 112,
  233.  128, 187,  21,  92,   5,  40,  44, 227, 149, 193,  21,  72, 228, 198, 109,  34,
  234.   72, 193,  19,  63, 199,  15, 134, 220,   7, 249, 201, 238,  65,   4,  31,  15,
  235.   64,  71, 121, 164,  93, 136, 110,  23,  50,  95,  81, 235, 213, 155, 192, 209,
  236.  242, 188, 193, 143 , 65,  17,  53, 100,  37, 123, 120,  52,  96,  42, 156,  96,
  237.  223, 248, 232, 163,  31,  99, 108,  27,  14,  18, 180, 194,   2, 225,  50, 158,
  238.  175, 102,  79, 209, 202, 209, 129,  21, 107,  35, 149, 224,  51,  62, 146, 225,
  239.   59,  36,  11,  98, 238, 190, 185,  34, 133, 178, 162,  14, 230, 186,  13, 153,
  240.  222, 114,  12, 140,  45, 162, 247,  40, 208,  18, 120,  69, 149, 183, 148, 253,
  241.  100, 125,   8,  98, 231, 204, 245, 240,  84,  73, 163, 111, 135, 125,  72, 250,
  242.  195, 157, 253,  39, 243,  62, 141,  30,  10,  71,  99,  65, 153,  46, 255, 116,
  243.   58, 111, 110, 171, 244, 248, 253,  55, 168,  18, 220,  96, 161, 235, 221, 248,
  244.  153,  27, 225,  76, 219, 110, 107,  13, 198, 123,  85,  16, 109, 103,  44,  55,
  245.   39, 101, 212,  59, 220, 208, 232,   4, 241,  41,  13, 199, 204,   0, 255, 163,
  246.  181,  57,  15, 146, 105,  15, 237,  11, 102, 123, 159, 251, 206, 219, 125, 156,
  247.  160, 145, 207,  11, 217,  21,  94, 163, 187,  19,  47, 136,  81,  91, 173,  36,
  248.  123, 148, 121, 191, 118,  59, 214, 235,  55,  57,  46, 179, 204,  17,  89, 121,
  249.  128,  38, 226, 151, 244,  46,  49,  45, 104,  66, 173, 167, 198, 106,  43,  59,
  250.   18, 117,  76, 204, 120,  46, 241,  28, 106,  18,  66,  55, 183, 146,  81, 231,
  251.    6, 161, 187, 230,  75, 251,  99,  80,  26, 107,  16,  24,  17, 202, 237, 250,
  252.   61,  37, 189, 216, 226, 225, 195, 201,  68,  66,  22,  89,  10,  18,  19, 134,
  253.  217,  12, 236, 110, 213, 171, 234,  42, 100, 175, 103,  78, 218, 134, 168,  95,
  254.  190, 191, 233, 136, 100, 228, 195, 254, 157, 188, 128,  87, 240, 247, 192, 134,
  255.   96, 120, 123, 248,  96,   3,  96,  77, 209, 253, 131,  70, 246,  56,  31, 176,
  256.  119,  69, 174,   4, 215,  54, 252, 204, 131,  66, 107,  51, 240,  30, 171, 113,
  257.  176, 128,  65, 135,  60,   0,  94,  95, 119, 160,  87, 190, 189, 232, 174,  36,
  258.   85,  70,  66, 153, 191,  88,  46,  97,  78,  88, 244, 143, 242, 221, 253, 162,
  259.  244, 116, 239,  56, 135, 137, 189, 194,  83, 102, 249, 195, 200, 179, 142, 116,
  260.  180, 117, 242,  85,  70, 252, 217, 185, 122, 235,  38,  97, 139,  29, 223, 132,
  261.  132, 106,  14, 121, 145,  95, 149, 226,  70, 110,  89, 142,  32, 180,  87, 112,
  262.  140, 213,  85, 145, 201,   2, 222,  76, 185,  11, 172, 225, 187, 130,   5, 208,
  263.   17, 168,  98,  72, 117, 116, 169, 158, 183, 127,  25, 182, 224, 169, 220,   9,
  264.  102,  45,   9, 161, 196,  50,  70,  51, 232,  90,  31,   2,   9, 240, 190, 140,
  265.   74, 153, 160,  37,  29, 110, 254,  16,  26, 185,  61,  29,  11, 165, 164, 223,
  266.  161, 134, 242,  15,  40, 104, 241, 105, 220, 183, 218, 131,  87,  57,   6, 254,
  267.  161, 226, 206, 155,  79, 205, 127,  82,  80,  17,  94,   1, 167,   6, 131, 250,
  268.  160,   2, 181, 196,  13, 230, 208,  39, 154, 248, 140,  39, 119,  63, 134,  65,
  269.  195,  96,  76,   6,  97, 168,   6, 181, 240,  23, 122,  40, 192, 245, 134, 224,
  270.    0,  96,  88, 170,  48, 220, 125,  98,  17, 230, 158, 215,  35,  56, 234,  99,
  271.   83, 194, 221, 148, 194, 194,  22,  52, 187, 203, 238,  86, 144, 188, 182, 222,
  272.  235, 252, 125, 161, 206,  89,  29, 118, 111,   5, 228,   9,  75, 124,   1, 136,
  273.   57, 114,  10,  61, 124, 146, 124,  36, 134, 227, 114,  95, 114,  77, 157, 185,
  274.   26, 193,  91, 180, 211, 158, 184, 252, 237,  84,  85, 120,   8, 252, 165, 181,
  275.  216,  61, 124, 211,  77, 173,  15, 196,  30,  80, 239,  94, 177,  97, 230, 248,
  276.  162, 133,  20, 217, 108,  81,  19,  60, 111, 213, 199, 231,  86, 225,  78, 196,
  277.   54,  42, 191, 206, 221, 198, 200,  55, 215, 154,  50,  52, 146,  99, 130,  18,
  278.  103,  14, 250, 142,  64,  96,   0, 224,  58,  57, 206,  55, 211, 250, 245, 207,
  279.  171, 194, 119,  55,  90, 197,  45,  27,  92, 176, 103, 158,  79, 163,  55,  66,
  280.  211, 130,  39,  64, 153, 188, 155, 190, 213,  17, 142, 157, 191,  15, 115,  21,
  281.  214,  45,  28, 126, 199,   0, 196, 123, 183, 140,  27, 107,  33, 161, 144,  69,
  282.  178, 110, 177, 190, 106,  54, 110, 180,  87,  72, 171,  47, 188, 148, 110, 121,
  283.  198, 163, 118, 210, 101,  73, 194, 200,  83,  15, 248, 238,  70, 141, 222, 125,
  284.  213, 115,  10,  29,  76, 208,  77, 198,  41,  57, 187, 219, 169, 186,  70,  80,
  285.  172, 149,  38, 232, 190,  94, 227,   4, 161, 250, 213, 240, 106,  45,  81, 154,
  286.   99, 239, 140, 226, 154, 134, 238,  34, 192, 137, 194, 184,  67,  36,  46, 246,
  287.  165,  30,   3, 170, 156, 242, 208, 164, 131, 192,  97, 186, 155, 233, 106,  77,
  288.  143, 229,  21,  80, 186, 100,  91, 214,  40,  38, 162, 249, 167,  58,  58, 225,
  289.   75, 169, 149, 134, 239,  85,  98, 233, 199,  47, 239, 211, 247,  82, 247, 218,
  290.   63,   4, 111, 105, 119, 250,  10,  89, 128, 228, 169,  21, 135, 176, 134,   1,
  291.  155,   9, 230, 173,  59,  62, 229, 147, 233, 144, 253,  90, 158,  52, 215, 151,
  292.   44, 240, 183, 217,   2,  43, 139,  81, 150, 213, 172,  58,   1, 125, 166, 125,
  293.  209, 207,  62, 214, 124, 125,  45,  40,  31, 159,  37, 207, 173, 242, 184, 155,
  294.   90, 214, 180, 114,  90, 136, 245,  76, 224,  41, 172, 113, 224,  25, 165, 230,
  295.   71, 176, 172, 253, 237, 147, 250, 155, 232, 211, 196, 141,  40,  59,  87, 204,
  296.  248, 213, 102,  41, 121,  19,  46,  40, 120,  95,   1, 145, 237, 117,  96,  85,
  297.  247, 150,  14,  68, 227, 211,  94, 140,  21,   5, 109, 212, 136, 244, 109, 186,
  298.    3, 161,  97,  37,   5, 100, 240, 189, 195, 235, 158,  21,  60, 144,  87, 162,
  299.  151,  39,  26, 236, 169,  58,   7,  42,  27,  63, 109, 155,  30,  99,  33, 245,
  300.  245, 156, 102, 251,  38, 220, 243,  25, 117,  51, 217,  40, 177,  85, 253, 245,
  301.    3,  86,  52, 130, 138, 186,  60, 187,  40,  81, 119,  17, 194,  10, 217, 248,
  302.  171, 204,  81, 103, 204, 173, 146,  95,  77, 232,  23,  81,  56,  48, 220, 142,
  303.   55, 157,  88,  98, 147,  32, 249, 145, 234, 122, 144, 194, 251,  62, 123, 206,
  304.   81,  33, 206, 100, 119,  79, 190,  50, 168, 182, 227, 126, 195,  41,  61,  70,
  305.   72, 222,  83, 105, 100,  19, 230, 128, 162, 174,   8,  16, 221, 109, 178,  36,
  306.  105, 133,  45, 253,   9,   7,  33, 102, 179, 154,  70,  10, 100,  69, 192, 221,
  307.   88, 108, 222, 207,  28,  32, 200, 174,  91, 190, 247, 221,  27,  88, 141,  64,
  308.  204, 210,   1, 127, 107, 180, 227, 187, 221, 162, 106, 126,  58,  89, 255,  69,
  309.   62,  53,  10,  68, 188, 180, 205, 213, 114, 234, 206, 168, 250, 100, 132, 187,
  310.  141, 102,  18, 174, 191,  60, 111,  71, 210, 155, 228,  99,  84,  47,  93, 158,
  311.  174, 194, 119,  27, 246,  78,  99, 112, 116,  14,  13, 141, 231,  91,  19,  87,
  312.  248, 114,  22, 113, 175,  83, 125,  93,  64,  64, 203,   8,  78, 180, 226, 204,
  313.   52, 210,  70, 106,   1,  21, 175, 132, 225, 176,   4,  40, 149, 152,  58,  29,
  314.    6, 184, 159, 180, 206, 110, 160,  72, 111,  63,  59, 130,  53,  32, 171, 130,
  315.    1,  26,  29,  75,  39, 114,  39, 248,  97,  21,  96, 177, 231, 147,  63, 220,
  316.  187,  58, 121,  43,  52,  69,  37, 189, 160, 136,  57, 225,  81, 206, 121,  75,
  317.   47,  50, 201, 183, 160,  31, 186, 201, 224,  28, 200, 126, 188, 199, 209, 246,
  318.  207,   1,  17, 195, 161, 232, 170, 199,  26, 144, 135,  73, 212,  79, 189, 154,
  319.  208, 218, 222, 203, 213,  10, 218,  56,   3,  57, 195,  42, 198, 145,  54, 103,
  320.  141, 249,  49, 124, 224, 177,  43,  79, 247, 158,  89, 183,  67, 245, 187,  58,
  321.  242, 213,  25, 255,  39, 217,  69, 156, 191, 151,  34,  44,  21, 230, 252,  42,
  322.   15, 145, 252, 113, 155, 148,  21,  37, 250, 229, 147,  97, 206, 182, 156, 235,
  323.  194, 168, 100,  89,  18, 186, 168, 209, 182, 193,   7,  94, 227,   5, 106,  12,
  324.   16, 210,  80, 101, 203,   3, 164,  66, 224, 236, 110,  14,  22, 152, 219,  59,
  325.   76, 152, 160, 190,  50, 120, 233, 100, 159,  31, 149,  50, 224, 211, 146, 223,
  326.  211, 160,  52,  43, 137, 113, 242,  30,  27,  10, 116,  65,  75, 163,  52, 140,
  327.  197, 190, 113,  32, 195, 118,  50, 216, 223,  53, 159, 141, 155, 153,  47,  46,
  328.  230,  11, 111,  71,  15, 227, 241,  29, 229,  76, 218,  84,  30, 218, 216, 145,
  329.  206,  98, 121, 207, 205,  62, 126, 111,  22,  24, 177, 102, 253,  44,  29,   5,
  330.  132, 143, 210, 197, 246, 251,  34, 153, 245,  35, 243,  87, 166,  50, 118,  35,
  331.  147, 168,  53,  49,  86, 204, 205,   2, 172, 240, 129,  98,  90, 117, 235, 181,
  332.  110,  22,  54, 151, 136, 210, 115, 204, 222, 150,  98, 146, 129, 185,  73, 208,
  333.   76,  80, 144,  27, 113, 198,  86,  20, 230, 198, 199, 189,  50, 122,  20,  10,
  334.   69, 225, 208,   6, 195, 242, 123, 154, 201, 170,  83, 253,  98, 168,  15,   0,
  335.  187,  37, 191, 226,  53, 189, 210, 246, 113,  18, 105,   5, 178,   4,   2,  34,
  336.  182, 203, 207, 124, 205, 118, 156,  43,  83,  17,  62, 192,  22,  64, 227, 211,
  337.   56, 171, 189,  96,  37,  71, 173, 240, 186,  56,  32, 156, 247,  70, 206, 118,
  338.  119, 175, 161, 197,  32, 117,  96,  96, 133, 203, 254,  78, 138, 232, 141, 216,
  339.  122, 170, 249, 176,  76, 249, 170, 126,  25,  72, 194,  92,   2, 251, 138, 140,
  340.    1, 195, 106, 228, 214, 235, 225, 249, 144, 212, 248, 105, 166,  92, 222, 160,
  341.   63,   9,  37,  45, 194,   8, 230, 159, 183,  78,  97,  50, 206, 119, 226,  91,
  342.   87, 143, 223, 227,  58, 195, 114, 230, 184,  58, 203,   2,  32,   2,  57, 122,
  343.  110, 198, 251,  91, 255, 207, 212, 221,  76, 191,  94, 209, 244,  63, 229, 130,
  344.   62, 244, 232,  35,  45,  21,  42, 240, 231,  24, 201, 112,  89, 189, 152,  32,
  345.   31,  74, 157,  98, 231, 165,  41, 186, 137, 225,  36, 141,  59, 248, 134,  86,
  346.  197,  17,  77,  14, 188,  76, 238,  22,   3,  77, 138,  57,  32, 228, 120, 130,
  347.  233, 174, 143, 189, 227, 171, 220,  31, 109, 165,  30,  82,  93, 178, 186, 225,
  348.    1, 248, 110, 122, 109, 156, 104, 169,  39,   8, 252, 217,  41,  60, 188,  12,
  349.  176,  60, 134, 248, 168, 173,  44,  47,   0,  66,  78, 235, 202, 203,  69,  45,
  350.  137, 204, 113, 252, 213, 156, 127, 145, 127,   6,  34, 188, 109, 138,   8, 177,
  351.  131,  77,  33,  50, 104, 132, 202, 130, 227, 170, 203, 243, 119, 134, 242, 250,
  352.   44, 171, 110,  61, 206,  83,  90, 209, 242,  10, 198,   7, 198, 184, 225,  79,
  353.   94, 180,  56, 142, 119,  80,  20, 166, 101, 102, 101, 247, 182,  74,  67, 228,
  354.  186,  56,  61,   1, 178, 228,  16, 121, 142, 178, 152, 111, 144, 158,  12, 164,
  355.   31, 123,  55, 119,  44,  18,  96,  48, 133,   8, 135,  24, 196, 231, 209, 189,
  356.   64, 101, 255, 206, 131, 146, 253, 138, 170,  54, 209,  43, 180, 200, 201, 208,
  357.  153,  79, 176, 183,  20, 249, 104,  24, 249, 165,  57, 152, 160, 161, 120, 198,
  358.   38, 132, 168,  30, 138, 233, 114, 246, 184,  66,  94, 182, 122,  41, 212, 134,
  359.   85,  27, 215,  25, 175,  50, 193, 137, 213,  20,  85,   5, 220, 129, 213,  62,
  360.   72,  66,  78, 218, 183, 150, 239,  70, 160,  73, 143,   3, 102, 125, 238, 222,
  361.    3, 172,  10, 179, 196, 151, 115,  61,  83,  22, 168, 145,  48, 168, 143, 204,
  362.  150,   4,  68,  10, 206, 235, 137,  58, 119,  37, 184,  43,  14,  30, 246, 157,
  363.   48,  42,  92, 142, 231, 184,  77, 239,  90,  49, 176, 150, 201, 235, 248, 141,
  364.   81,  45, 120, 142, 126,  64,   2, 238, 135, 224,  42, 246, 195,  88, 161, 187,
  365.    2, 232, 215, 175, 223, 159, 176, 231, 121,  14, 148,  42,  59,  60,  26, 186,
  366.  198, 255, 167, 175, 157, 247, 150, 249,  50,  27, 185, 148,   1, 116, 168, 168,
  367.  237,  34,  22,  44, 207, 241, 187, 153, 218, 168, 213,  81, 164, 213, 228,  75,
  368.  236, 221, 227, 236, 168,  13, 197,   9,   3, 147, 238, 242, 114,  82,  61,  49,
  369.  212, 142,  58,  28,  34,  78, 182,  94,  96,  82, 195, 164,  33,   9, 195,  47,
  370.    5,  46, 227, 136, 237, 159, 126, 169, 145, 198,  47, 151, 119, 181,  91, 160,
  371.   21,  12, 188, 163,  58, 236, 101,  37, 223,  49, 131, 131,  67, 169, 206,  38,
  372.  147,  98, 173, 139,   1,  52,  20,  11, 141, 245, 207, 129,  30, 159, 245,  89,
  373.   22, 127,   5, 100,  56,  18, 244, 224,  88, 138,  82, 176, 203, 184, 233,  68,
  374.  239,  91,  22, 163, 115, 196, 237, 161, 125, 252, 254, 234, 245,  75, 203, 190,
  375.  135, 115, 227, 210, 197,  49, 220, 208,  85, 196, 103,  41,  82, 119,  79,  58,
  376.   87, 202, 107, 192,  70, 125,  58,  59,  36, 119, 132,  37, 183, 153,  30, 154,
  377.  221, 130,  92,  38, 228,  82, 200, 238, 252, 172, 222,  30, 132, 131,  58, 243,
  378.   97,  33,  29,   3,  23,  50, 193,  49, 204, 173, 178,  71, 230,   6, 190, 140,
  379.  113,  43,  57, 241, 136, 180, 239,  57,  58, 159, 205, 197, 197, 117,  81, 105,
  380.   31, 246, 153,  79,  57, 130, 156, 176,  17,   1, 101, 115,  51,  67, 203, 235,
  381.   97, 211, 208, 180,  68, 243,  10, 239, 168, 174, 115, 117,  42,  58,  28, 157,
  382.  180, 183,   9,  20, 214, 171,  37,  12, 133,  59, 115,  40,  73,  95, 148, 143,
  383.  210, 164, 237, 142, 108, 247,  81, 228, 195,  32, 187, 117, 217, 202, 160, 179,
  384.  139, 165,  98,  98,  78, 132, 176,  63, 238, 168,   7, 110, 116, 160, 127, 229,
  385.  128,  57, 224,  12,  54, 255, 218, 248,   3, 115,  19,  88, 185, 230, 113, 185,
  386.  218, 196, 206,  28, 178,  91,  16, 237,  77, 211, 213, 177, 252, 242, 180, 128,
  387.   70,  52, 245, 121,  37, 234, 196,   0, 169, 172,  85, 234, 114, 137,  50, 223,
  388.    6,   4,  29,   5,  93,  49, 245,   2, 197,  57, 194, 227,  43, 137, 217, 219,
  389.   91, 204,  10, 152, 192,  91, 253, 111,  27,  37,   6,  34,  46,  33, 190,  14,
  390.   96, 151,  59,   4, 236, 213,  74, 103, 181,  79, 230,  56, 166, 237, 102,  21,
  391.  152,  26, 145,  10,  93, 146, 146, 141, 172, 111, 198, 151, 231,  60,  99, 173,
  392.   69, 110, 223,  95,  69, 122, 129,  69,  81, 135,  90, 100, 205,  48, 153, 241,
  393.  105, 181, 241, 138, 140, 115, 238,  11,  94,  87,  54, 143, 108, 121, 244, 187,
  394.  122,  89,  89,  38, 170, 180, 158, 198, 138, 200, 252, 251));
  395.  
  396. PROCEDURE InitBlowFish(Key : String; var P : PArray; var S : SBox);
  397.  
  398.   var
  399.     i, j, k            : Byte;
  400.     l                  : Integer;
  401.     Data, Datal, Datar : LongInt;
  402.  
  403.   begin
  404.  
  405.   { Initialize first the P-array... }
  406.  
  407.     for i := 0 to (bf_N+1) do P[i] := RandomSeed.long[i];
  408.  
  409.   { ...and then the four S-boxes, in order, with a fixed random string. }
  410.   { This string consists of the hexadecimal digits of Pi.               }
  411.  
  412.     l := PBoxSizeL;
  413.     for i := 0 to 3 do
  414.       begin
  415.         for j := 0 to 255 do S[i, j] := RandomSeed.long[l+j];
  416.         l := l+256;
  417.       end;
  418.  
  419.   { XOR P1 with the first 32 bits of the key, XOR P2 with the second    }
  420.   { 32 bits of the key, and so on for all bits of the key (up to P18).  }
  421.   { Cycle throught the key bits repeatedly until the entire P-array     }
  422.   { has been XORed.                                                     }
  423.  
  424.     j := 1;
  425.     for i := 0 to (bf_N+1) do
  426.       begin
  427.         Data := bf_IV; { In the original algorithm this vector was zero }
  428.         for k := 0 to 3 do
  429.           begin
  430.             Data := ((Data shl 8) or Ord(Key[j]));
  431.             Inc(j);
  432.             if j > Length(Key) then j := 1;
  433.           end;
  434.         P[i] := P[i] xor Data;
  435.       end;
  436.  
  437.   { Encrypt the all-zero string using the Blowfish algorithm using     }
  438.   { the subkeys described in steps #1 and #2 and replace the elements  }
  439.   { of the P-array with the output of this process.                    }
  440.  
  441.     Datal := bf_IV;    { In the original algorithm this vector was zero }
  442.     Datar := bf_IV;    { In the original algorithm this vector was zero }
  443.     for i := 0 to (bf_N+1) do
  444.       begin
  445.         BlowEncrypt(Datal, Datar, P, S);
  446.         P[i] := Datal;
  447.         P[i+1] := Datar;
  448.         Inc(i);
  449.       end;
  450.  
  451.   { Continue the process, replacing the elements of the four S-boxes in    }
  452.   { order, with the output of the continuously changing Blowfish algorithm }
  453.  
  454.     for j := 0 to 3 do
  455.       begin
  456.         for i := 0 to 255 do
  457.           begin
  458.             BlowEncrypt(Datal, Datar, P, S);
  459.             S[j, i] := Datal;
  460.             S[j, i+1] := Datar;
  461.             Inc(i);
  462.           end;
  463.       end;
  464.  
  465.   { In total, 521 iterations are required to generate all required subkeys. }
  466.   { ...Applications can store the subkeys rather than re-executing all this }
  467.   { derivation on process.                                                  }
  468.  
  469.   end;
  470.  
  471.  
  472.  
  473. {$IFDEF Use_Assembler}
  474.  
  475.   ERROR - Assembler Routines are not available in the public
  476.   ~~~~~   domain package... IF you wish the fastest routines
  477.           in the Universe, for the Blowfish Algorithm,  THEN
  478.           please contact me for details.
  479.  
  480.           You still can confirm the equality of both outputs
  481.           since the executable uses my 8086 ASM routines.
  482.  
  483.                   E-MAIL to :  dutra.lacerda@mail.telepac.pt
  484.  
  485. {$ELSE}
  486.  
  487. FUNCTION F(Input:LongInt;S:SBox):LongInt;
  488.  VAR
  489.     PInput:PLArray;
  490.  
  491.  BEGIN
  492.       PInput:=@Input;
  493.       F:= ((S[0,PInput^[3]]  +  S[1,PInput^[2]])
  494.                             XOR S[2,PInput^[1]])
  495.                              +  S[3,PInput^[0]];
  496.  
  497.       { In the original article in Dr. Dobb's Journal this function used the
  498.         PBox from 0 to 3... I choose to invert it. The security is the same.
  499.         ...Also...
  500.         In the original article in Dr. Dobb's Journal this function included
  501.         a MOD 2^32 (which  wouldn't fit in a 32 bits integer anyway), in the
  502.         source there was a MOD (2^32 - 1) while in the C source available on
  503.         Dr. Dobb's  official FTP site on  ftp.mv.com there wasn't any MOD to
  504.         be found. After  he was asked about this phenomenon,  Bruce Schneier
  505.         stated that it was not needed anyway. In this source it is left out-
  506.         commented for completeness.
  507.       }
  508.  END;
  509.  
  510.  
  511. PROCEDURE BlowEncrypt(VAR Xl,Xr:LongInt;P:PArray;S:SBox);
  512.  VAR
  513.     i  : BYTE;
  514.     _Xl: Array[0..3] of Byte Absolute Xl;
  515.     _Xr: Array[0..3] of Byte Absolute Xr;
  516.  
  517.  BEGIN
  518.  
  519.    Xl := Xl xor P[0];
  520.  
  521.    { 16 Rounds to go: Realy 8 Double rounds to avoid swaps }
  522.    FOR i:=1 TO (bf_N) DO
  523.       BEGIN
  524.       { This is a Readable version ...
  525.       Xr:=F(Xl,S) xor Xr xor P[i];
  526.       Xl:=F(Xr,S) xor Xl xor P[i+1];
  527.       inc(i);
  528.       ... do not Uncomment! }
  529.  
  530.       { In the original article in Dr. Dobb's Journal this function }
  531.       { used the PBox from 0 to  3...  I choose to invert it.       }
  532.       { The security is the same and MAY increase peekers confusion }
  533.  
  534.       { Faster First Half Round }
  535.       Xr:= Xr xor P[i] xor (((
  536.            S[0,_Xl[3]] + S[1,_Xl[2]])
  537.            xor S[2,_Xl[1]]) + S[3,_Xl[0]]);
  538.  
  539.       { Faster Second Half Round }
  540.       Xl:= Xl xor P[i+1] xor (((
  541.            S[0,_Xr[3]] + S[1,_Xr[2]])
  542.            xor S[2,_Xr[1]]) + S[3,_Xr[0]]);
  543.  
  544.       inc(i);
  545.  
  546.       END;
  547.  
  548.    Xr:=Xr XOR P[(bf_N+1)];
  549.  
  550.  END;
  551.  
  552.  
  553. PROCEDURE BlowDecrypt(VAR Xl,Xr:LongInt;P:PArray;S:SBox);
  554.  VAR
  555.     i:BYTE;
  556.     _Xl: Array[0..3] of Byte Absolute Xl;
  557.     _Xr: Array[0..3] of Byte Absolute Xr;
  558.  
  559.  BEGIN
  560.    Xr := Xr xor P[(bf_N+1)];
  561.  
  562.    { 16 Rounds to go: Realy 8 Double rounds to avoid swaps }
  563.    FOR i:=bf_N DOWNTO 1 DO
  564.       BEGIN
  565.       { This is a Readable version ...
  566.       Xl:=F(Xr,S) xor Xl xor P[i];
  567.       Xr:=F(Xl,S) xor Xr xor P[i-1];
  568.       dec (i);
  569.       ... do not Uncomment! }
  570.  
  571.       { In the original article in Dr. Dobb's Journal this function }
  572.       { used the PBox from 0 to  3...  I choose to invert it.       }
  573.       { The security is the same and MAY increase peekers confusion }
  574.  
  575.       { Faster First Half Round }
  576.       Xl:= Xl xor P[i] xor (((
  577.            S[0,_Xr[3]] + S[1,_Xr[2]])
  578.            xor S[2,_Xr[1]]) + S[3,_Xr[0]]);
  579.  
  580.       { Faster Second Half Round }
  581.       Xr:= Xr xor P[i-1] xor (((
  582.            S[0,_Xl[3]] + S[1,_Xl[2]])
  583.            xor S[2,_Xl[1]]) + S[3,_Xl[0]]);
  584.  
  585.       dec (i);
  586.  
  587.       END;
  588.  
  589.    Xl:=Xl xor P[0];
  590.  
  591.  END;
  592.  
  593. {$ENDIF}
  594.  
  595. begin
  596. end.